package com.itheima.mall.api.oms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.mall.mapper.oms.ReturnReasonMapper;
import com.itheima.mall.pojo.oms.OmsOrderReturnReason;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class ReturnReasonApiImpl implements ReturnReasonApi{
    @Autowired
    private ReturnReasonMapper returnReasonMapper;

    @Override
    public void createReason(OmsOrderReturnReason omsOrderReturnReason) {
        returnReasonMapper.insert(omsOrderReturnReason);
    }

    @Override
    public void deleteReason(Integer[] ids) {
        for (Integer id : ids) {
            Long reasonId = Convert.toLong(id);
            LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OmsOrderReturnReason::getId,reasonId);
            returnReasonMapper.delete(wrapper);
        }
    }

    @Override
    public PageResult findReason(Integer pageNum, Integer pageSize) {
        Page pageCondition = new Page(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> lqw = new LambdaQueryWrapper<>();
        Page pageR = returnReasonMapper.selectPage(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));
        return pageResult;
    }

    @Override
    public void updateStatus(Integer[] ids, Integer status) {
        for (Integer id : ids) {
            Long updateStatusId = Convert.toLong(id);
            OmsOrderReturnReason omsOrderReturnReason = returnReasonMapper.selectById(updateStatusId);
            omsOrderReturnReason.setStatus(status);
            returnReasonMapper.updateById(omsOrderReturnReason);
        }
    }

    @Override
    public void updateReason(OmsOrderReturnReason omsOrderReturnReason,Integer id) {
        omsOrderReturnReason.setId(Long.valueOf(id));
        returnReasonMapper.updateById(omsOrderReturnReason);
    }

    @Override
    public OmsOrderReturnReason getOneReason(Integer id) {
        return returnReasonMapper.selectById(id);
    }
}
